// pages/myInfo/myInfo.js
const app = getApp()
import {
  postRequest,
  getRequest,
  postParamsRequest
} from '../../api/request';
import {
  showTips,
  showModal,
} from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeColor: app.globalData.themeColor,
    subColor: app.globalData.subColor,
    btnColor: app.globalData.btnColor,
    myIndexHeadImg1: app.globalData.myIndexHeadImg1,
    myIndexHeadImg2: app.globalData.myIndexHeadImg2,
    myBirthday: '2000-01-01',
    myBirthday2: "请选择",
    avatar: "",
    nickname: "",
    mobile: "",
    gender: 0,
    birthday: "",
    genderList: [{
        id: 1,
        name: "先生",
        checked: false,
      },
      {
        id: 2,
        name: "女士",
        checked: false,
      },
    ],
    //提交锁
    submitLock: false,
  },

  //昵称改变时
  changeNickname(e) {
    this.setData({
      nickname: e.detail.value
    })
  },

  //性别单选框改变时
  radioChange(e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)

    const items = this.data.genderList
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].id == e.detail.value;
      if (items[i].checked) {
        this.setData({
          gender: items[i].id,
        });
      }
    }
    this.setData({
      genderList: items
    })
  },

  //点击性别文字
  genderChange(e){
    console.log(e);
    const items = this.data.genderList;
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].id == e.currentTarget.dataset.id;
      if (items[i].checked) {
        this.setData({
          gender: items[i].id,
        });
      }
    }
    this.setData({
      genderList: items
    })
  },

  //生日改变时
  bindDateChange: function (e) {
    this.setData({
      myBirthday: e.detail.value,
      myBirthday2: e.detail.value,
      birthday: e.detail.value,
    })
  },

  //手机号码自动填写
  getPhoneNumber(e) {
    console.log(e);
    if (e.detail.errMsg != 'getPhoneNumber:ok') {
      return
    }
    let param = {
      iv: e.detail.iv,
      encryptedData: e.detail.encryptedData,
      memberId: app.myInfo.memberId,
    };
    postParamsRequest("/api/vip/getPhoneNumber", param).then((res) => {
      console.log(res);
      let data = res.data.data
      if (data.phoneNumber) {
        postParamsRequest("/api/vip/updateMemberPhone", {
          memberId: app.myInfo.memberId,
          phone: data.phoneNumber,
        }).then((res) => {
          wx.showToast({
            title: '更换成功',
            icon: 'none',
          })
          this.setData({
            mobile:data.phoneNumber,
          });
        }).catch((err) => {

        });
      }

    }).catch((err) => {});
  },

  //头像
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    this.setData({
      avatar: avatarUrl,
    })
    let self = this
    wx.uploadFile({
      url: app.API_HOST + '/upload/files',
      formData: {
        token: app.token,
      },
      filePath: avatarUrl,
      name: 'file',
      success: function (res) {
        res.data = JSON.parse(res.data);
        self.setData({ avatar: res.data.data })
      },
      fail: function (res) {

      }
    });
  },

  //保存个人信息
  saveUserInfo() {

    if (this.data.nickname == '') {
      showTips('昵称不能为空', 'none')
      return
    }

    let param = {
      memberId: app.myInfo.memberId,
      avatarUrl: this.data.avatar,
      birthDay: this.data.birthday,
      gender: this.data.gender,
      nickName: this.data.nickname,
    };
    console.log(param);
   
    this.setData({
      submitLock: true,
    })
    postParamsRequest("/api/vip/updateUserInfo", param).then((res) => {
      console.log(res);
      wx.showToast({
        title: '保存成功',
        icon: 'none',
      })
      this.setData({
        submitLock: false
      })
    }).catch((err) => {
      this.setData({
        submitLock: false
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let userInfo = wx.getStorageSync('userInfo');
    console.log(userInfo);
    //初始化性别
    let items = this.data.genderList
    for (let i = 0, len = items.length; i < len; ++i) {
      items[i].checked = items[i].id == userInfo.gender;
    }
    //默认头像
    if (userInfo.avatar == '') {
      if (userInfo.gender == 2) {
        userInfo.avatar = this.data.myIndexHeadImg2;
      } else {
        userInfo.avatar = this.data.myIndexHeadImg1;
      }
    }
    this.setData({
      avatar: userInfo.avatar,
      nickname: userInfo.nickname,
      mobile: userInfo.mobile,
      gender: userInfo.gender,
      birthday: userInfo.birthday != null &&  userInfo.birthday != '' ? userInfo.birthday : '',
      genderList: items,
      myBirthday: userInfo.birthday != null &&  userInfo.birthday != '' ? userInfo.birthday : '2000-01-01',
      myBirthday2: userInfo.birthday != null &&  userInfo.birthday != '' ? userInfo.birthday : '请选择',
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})